#pragma once
#include<assert.h>
#include<iostream>
using namespace std;
namespace why
{
	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> node;
		typedef __list_iterator<T, Ref,Ptr> self;
		node* _node;
		__list_iterator(node* n)
			:_node(n)
		{}
		Ref& operator*()
		{
			return _node->_data;
		}
		self operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		self operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
	};
	/*template<class T>
	struct __list_const_iterator
	{
		typedef list_node<T> node;
		typedef __list_const_iterator<T> self;
		node* _node;
		__list_const_iterator(node* n)
			:_node(n)
		{}
		const T& operator*()
		{
			return _node->_data;
		}
		self operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		self operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
	};*/
	template<class T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list()
		{
			empty_init();
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
		}
		list(const list<T>& lt)
		{
			empty_init();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		list<int>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				 erase(it++);
				//it = erase(it);
			}
		}
		iterator begin()
		{
			//iterator it(_head->_next);
			//return it;
			return iterator(_head->_next);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator end() const
		{
			//iterator it(_head->_next);
			//return it;
			return const_iterator(_head);
		}
		void push_back(const T& x)
		{
			/*node* tail = _head->_prev;
			node* newnode = new node(x);
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void insert(iterator pos, const T& x)
		{
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* newnode = new node(x);
			prev->_next = newnode;
			newnode->_prev = prev;
			cur->_prev = newnode;
			newnode->_next = cur; 
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._node->_prev;
			node* cur = pos._node->_next;
			prev->_next = cur;
			cur->_prev = prev;
			delete pos._node;
			return iterator(cur);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
	private:
		node* _head;
	};
	void print(const list<int>& t)
	{
		list<int>::const_iterator it = t.begin();
		while (it != t.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}
	void test1()
	{
		list<int> ll;
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		list<int>::iterator it = ll.begin();
		while (it != ll.end())
		{
			 cout << *it << ' ';
			 ++it;
		}
		cout << endl;
		print(ll);
	}
	void test2()
	{
		list<int> ll;
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		list<int>::iterator pos = ll.begin();
		ll.insert(pos, 20);
		print(ll);
	}
	void test3()
	{
		list<int> ll;
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		ll.push_back(1);
		print(ll);
		ll.clear();
		print(ll);
		ll.push_back(10);
		ll.push_back(19);
		ll.push_back(18);
		ll.push_back(16);
		print(ll);

	}
}



